package com.pond.auth.service;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class SmsCodeService {

    private final StringRedisTemplate redisTemplate;

    public SmsCodeService(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    private static final String PREFIX = "sms_code:";
    private static final long EXPIRE_MINUTES = 5;

    public void saveCode(String phone, String code) {
        redisTemplate.opsForValue().set(PREFIX + phone, code, EXPIRE_MINUTES, TimeUnit.MINUTES);
    }

    public String getCode(String phone) {
        return redisTemplate.opsForValue().get(PREFIX + phone);
    }

    public boolean validateCode(String phone, String inputCode) {
        String code = getCode(phone);
        return inputCode != null && inputCode.equals(code);
    }

    public void deleteCode(String phone) {
        redisTemplate.delete(PREFIX + phone);
    }
}
